<template>
  <div>
    <p>定时任务</p>
    <el-col :span="24">
      <el-col :span="14">
        <el-form :inline="true" :model="query" class="demo-form-inline">
          <el-form-item>
            <el-input placeholder="请输入bean名称" v-model="query.beanName" prefix-icon="el-icon-document"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input placeholder="请输入执行任务" v-model="query.methodName" prefix-icon="el-icon-edit-outline"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="showData()" icon="el-icon-search">查询</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="10">
        <el-button type="success" icon="el-icon-plus" v-if="$store.state.authorities.includes('monitor:task:add')" @click="addSchedule()" style="float: right;margin-right: 60px">新增定时任务</el-button>
      </el-col>
    </el-col>
    <br>
    <br>
    <el-table
        :data="tableData"
        stripe
        style="width: 100%"
    >
      <el-table-column
          prop="jobId"
          label="任务ID"
      width="100px">
      </el-table-column>
      <el-table-column
          prop="beanName"
          label="bean名称">
      </el-table-column>
      <el-table-column
          prop="methodName"
          label="执行任务">
      </el-table-column>
      <el-table-column
          prop="cronExpression"
          label="cron表达式">
      </el-table-column>
      <el-table-column
          prop="status"
          label="任务状态">
        <template v-slot="scope">
          <el-switch
              v-model="scope.row.status"
              :active-value="0"
              :inactive-value="1"
              active-color="#13ce66"
              v-if="$store.state.authorities.includes('monitor:task:status')"
              @change="ChangeStatus(scope.row.jobId,scope.row.status)"
              inactive-color="#909399">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="创建时间">
      </el-table-column>
      <el-table-column
          prop="remark"
          label="描述">
      </el-table-column>
      <el-table-column
          label="操作" width="220px">
        <template v-slot="scope">
          <el-button type="primary" v-if="$store.state.authorities.includes('monitor:task:edit')" @click="editSchedule(scope.row)" icon="el-icon-edit" size="small">修改</el-button>
          <el-button type="danger" v-if="$store.state.authorities.includes('monitor:task:remove')" @click="deleteSchedule(scope.row.jobId)" icon="el-icon-delete" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <br>
    <el-pagination
        style="margin:0 auto;width: 100vh"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[8,20,30,40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">
      <el-form label-position="left" :inline="true" :model="form" class="demo-form-inline" :rules="rules"
               ref="formName">
        <el-form-item label="bean名称" :label-width="formWidth" prop="beanName">
          <el-input placeholder="请输入bean名称" v-model="form.beanName" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="执行任务" :label-width="formWidth" prop="methodName">
          <el-input placeholder="请输入执行任务" v-model="form.methodName" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="cron表达式" :label-width="formWidth" prop="cronExpression">
          <el-input placeholder="请输入cron表达式" v-model="form.cronExpression" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="描述" :label-width="formWidth" prop="remark">
          <el-input type="textarea" placeholder="请输入描述" v-model="form.remark" style="width: 300px"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitForm()">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "ScheduleTaskPage",
  data() {
    return {
      title:"",
      dialogVisible: false,
      pageSize:8,
      total:0,
      currentPage:1,
      query: {
        beanName: "",
        methodName: "",
      },
      form: {
        jobId: "",
        beanName: "",
        methodName: "",
        remark: "",
        cronExpression: "",
      },
      tableData: [],
      formWidth: "100px",
      rules: {
        beanName: [{required: true, message: 'SpringBean名称不能为空', trigger: 'blur'}],
        methodName: [{required: true, message: '执行的方法不能为空', trigger: 'blur'}],
        cronExpression: [{required: true, message: 'cron表达式不能为空', trigger: 'blur'}],
        remark: [{required: true, message: '备注不能为空', trigger: 'blur'}]
      }
    }
  },
  methods: {
    handleClose(){
      this.dialogVisible =false;
    },
    addSchedule() {
      this.title = "添加定时任务";
      this.form={};
      this.dialogVisible = true;
    },
    ChangeStatus(id, status) {
      request("scheduleTask/task?id=" + id + "&status=" + status, "post",).then(()=>{
        if(status){
          this.$message.info("任务状态停用完成！")
        }else {
          this.$message.success("任务状态启用完成")
        }
      }).catch(()=>{
        this.$message.error("任务状态修改失败！！！")
      });

    },
    handleSizeChange(i){
      this.pageSize=i;
      this.currentPage=1;//一旦页面大小发生变化，必须回第一页
      this.showData();
    },
    handleCurrentChange(i){
      this.currentPage=i;
      this.showData();
    },
    submitForm() {
      this.$refs.formName.validate((valid) => {
        if (valid) {
          this.dialogVisible = false;
          request("scheduleTask", this.form.jobId ? "put" : "post", this.form)
          setTimeout(() => {
            this.showData();
          }, 1000)
        } else {
          this.$message.error('表单校验失败');
        }
      })
    },
    editSchedule(row) {
      this.title = "编辑定时任务";
      this.dialogVisible = true;
      this.form = row;
    },
    deleteSchedule(id) {
      console.log(id);
      this.$confirm('此操作将永久删除这条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        request("scheduleTask?id=" + id, "delete")
        setTimeout(() => {
          this.currentPage=1;
          this.showData();
        }, 1000)
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(()=>{
        console.log("取消")
      })
    },
    async showData() {
      let data = await request(`scheduleTask?size=${this.pageSize}&page=${this.currentPage}&beanName=${this.query.beanName}&methodName=${this.query.methodName}`);
      this.tableData = data.list;
      this.total = data.total;
    }
  },
  created() {
    this.showData();
  }
}
</script>

<style scoped>

</style>